package dk.dashboardFP.interactionElementFramwork.inputElement {
	import dk.dashboardFP.interactionElementFramwork.InteractionElementViewEvent;
	import dk.dashboardFP.interactionElementFramwork.inputElementViews.CheckBoxView;

	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;

	/**
	 * @author jk
	 */
	public class CheckBox extends AbstractInputElement {

		private var _bValue : Boolean=true;
		private var _doCheckBoxView : CheckBoxView;
		private var _bAutoSetValue : Boolean=true;

		public function CheckBox(doIEV : CheckBoxView, bInitialValue : Boolean=false) {
			
			_doCheckBoxView = doIEV;
			
			super(doIEV);
			setupCheckBox();

			setValue(bInitialValue);
			
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setuå
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function setupCheckBox() : void {
			_doIEV.addEventListener(InteractionElementViewEvent.CLICK_VIEW, onViewClicked, false, 0, true);
			_doIEV.addEventListener(InteractionElementViewEvent.KEY_DOWN_VIEW, onKeyDownWhileInFocus, false, 0, true);
		}
		
		protected function onViewClicked(event : InteractionElementViewEvent) : void {
			
			if (bDisabled) {
				return;
			}
			
			if (_bAutoSetValue) {
				toggleValue(event.oOriginalEvent);
			} else {
				dispatchEvent(new CheckBoxEvent(CheckBoxEvent.REQUEST_VALUE_CHANGE, event.oOriginalEvent));
			}
		}
		
		private function toggleValue(oEvent : Event=null) : void {
			if (_bValue) {
				setValue(false, oEvent);
			} else {
				setValue(true, oEvent);
			}
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// key while in focus
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		protected function onKeyDownWhileInFocus(event : InteractionElementViewEvent) : void {
			if (KeyboardEvent(event.oOriginalEvent).charCode==Keyboard.SPACE) {
				toggleValue();
			}
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// set value
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function setValue(oNewValue : Boolean, oOriginalEvent : Event=null, bDispatchChangeEvent : Boolean=true) : void {
			_bValue = oNewValue;
			if (bDispatchChangeEvent) {
				dispatchEvent(new CheckBoxEvent(CheckBoxEvent.VALUE_CHANGED, oOriginalEvent));
			}
			updateView();
		}
		
		private function updateView() : void {
			
			if (_bValue) {
				_doCheckBoxView.setCheked();
			} else {
				_doCheckBoxView.setUnCheked();
			}
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// main value setter getter
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function get bValue() : Boolean { 
			return _bValue; 
		}

		public function set_value(bValue : Boolean, bDispatchEvent : Boolean=true) : void {
			if (_bValue!=bValue) {
				setValue(bValue, null, bDispatchEvent);
			}
		}
		
		public function get bAutoSetValue() : Boolean {
			return _bAutoSetValue;
		}
		
		public function set bAutoSetValue(bAutoSetValue : Boolean) : void {
			_bAutoSetValue = bAutoSetValue;
		}
	}
}
